package com.example.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import android.annotation.SuppressLint;

@SuppressLint("DefaultLocale")
public class DateUtil {

	public static TimeZone getTimeZone() {
		return TimeZone.getDefault();
	}
	
	public static Date getDate(int addDays) {
		Calendar cal = GregorianCalendar.getInstance(getTimeZone());
		cal.add(Calendar.DATE, addDays);
		return cal.getTime();
	}
	
	public static Date getCurrentDate() {
		return GregorianCalendar.getInstance(getTimeZone()).getTime();
	}

	public static String getTimestamp() {
		return getCurrentTime("yyyyMMddHHmmss");
	}
	
	public static String getTimestamp(int addDays) {
		return getTimestamp("yyyyMMddHHmmss", addDays);
	}
	
	public static String getTimestamp(String formatter, int addDays) {
		SimpleDateFormat fmt = new SimpleDateFormat(formatter, Locale.US);
		fmt.setTimeZone(getTimeZone());
		return fmt.format(getDate(addDays));
	}
	
	public static String getCurrentTime(String formatter) {
		SimpleDateFormat fmt = new SimpleDateFormat(formatter, Locale.US);
		fmt.setTimeZone(getTimeZone());
		return fmt.format(getCurrentDate());
	}

	public static String getCurrentDateString() {
		return getCurrentTime("yyyy/MM/dd");
	}

	public static String getToday() {
		return getCurrentTime("yyyyMMdd");
	}
	
	public static Date toDate(String timestamp) {
		return toDate(timestamp, "yyyyMMddHHmmss");
	}
	
	public static Date toDate(String timestamp, String formatter) {
		SimpleDateFormat fmt = new SimpleDateFormat(formatter, Locale.US);
		try {
			return fmt.parse(timestamp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getTimeInfo(long timeBase){
		int time = (int)(System.currentTimeMillis() - timeBase) / 1000;
		int hour = (int) (time/3600);
		int min = (int) (time/60);
		int sec= (int) (time%60);
		return  hour+":"+min+":"+sec;
	}
	
	public static String getTimeInfoMs(long timeBase){
		float time = (float)(System.currentTimeMillis() - timeBase) / 1000;
		String strtime = String.format("%03f",time);
		return  strtime;
	}

	public static boolean isExpired(String timestamp) {
		Date currentDate = getCurrentDate();
		Date expiredData = toDate(timestamp);
		
		return currentDate.compareTo(expiredData) > 0;
	}
}
